(function(){
	
	var map, currentInfoWindow;
	
	function initMap(canvas){    
		var myLatlng = new google.maps.LatLng(39.9529,116.394),
			myOptions = {
				zoom: 11,
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				mapTypeControlOptions: {mapTypeIds: []}
			};
		map = new google.maps.Map(document.getElementById(canvas), myOptions);	
	}
	
	function load(url){
		$.ajax({
			url: url,
			dataType: 'json',
			cache: false,
			success: function(json){
				if (json) {
					parse(json);
				}
			},
			error: function(){
				//load();
			}
		});
	}
	
	function setCenter(latlng){
		map.setCenter(latlng);
	}
	
	function parse(data){
		if (!!data && data.length != 0){
			var len = data.length, i = 0;
			for (; i < len; i++ ){
				var item = data[i];
				if (i == 0){
					var latlng = new google.maps.LatLng(item.latlng[0], item.latlng[1]);
					setCenter(latlng);
					//showInfoWindow(item);
				}
				createMarker(item);
			}
		}
	}
	function createMarker(item){
		var latlng = new google.maps.LatLng(item.latlng[0], item.latlng[1]),
			marker = new google.maps.Marker({
				position: latlng,
				map: map,
				icon: 'http://assets.digu.com/images/map_marker_new.png'
			});
		
		google.maps.event.addListener(marker, 'click', function() {
			showInfoWindow(item);
		});
	}
	
	function showInfoWindow(item){
		if (currentInfoWindow){
			currentInfoWindow.close();
		}
		var latlng = new google.maps.LatLng(item.latlng[0], item.latlng[1]);
		var infoWindow = new CustomInfoWindow({
			map: map,
			content: item.html,
			position: latlng
		});
		infoWindow.open();
		currentInfoWindow = infoWindow;
	}
		
	//initMap();
	
	$.extend({
		ShopMap: {
			initMap: initMap,
			load: load
		}
	});

})();